package com.ext.aic.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.StyleSpan;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.ext.aic.R;
import com.ext.aic.adapter.IndustryEnterpriseListAdapter;
import com.ext.aic.apis.gc.CompanyListRequest;
import com.ext.aic.beans.CompanyItem;
import com.ext.aic.beans.Pagination;
import com.ext.aic.beans.SecondIndustry;
import com.ext.aic.constants.EBusConstants;
import com.ext.aic.databinding.IndustryEnterpriseListLayoutBinding;
import com.ext.aic.handler.PageHandler;
import com.eyinfo.android_pure_utils.ebus.EBus;
import com.eyinfo.android_pure_utils.ebus.SubscribeEBus;
import com.eyinfo.android_pure_utils.utils.JsonUtils;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IndustryEnterpriseListActivity extends BaseActivity {

    private IndustryEnterpriseListLayoutBinding binding;
    private SecondIndustry secondIndustry;
    private IndustryEnterpriseListAdapter adapter;
    private String keyword = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initParams();
        View view = View.inflate(this, R.layout.industry_enterprise_list_layout, null);
        binding = IndustryEnterpriseListLayoutBinding.bind(view);
        initView();
        setContentView(binding.getRoot());
        EBus.getInstance().registered(this);
        pageHandler.refresh();
    }

    private void initParams() {
        Intent intent = getIntent();
        if (intent != null) {
            String industryJson = intent.getStringExtra("industry");
            secondIndustry = JsonUtils.parseT(industryJson, SecondIndustry.class);
        }
    }

    private void bindEnterpriseCount(int count) {
        String text = getString(R.string.enterprise_count_format, String.valueOf(count));
        SpannableStringBuilder ssb = new SpannableStringBuilder(text);
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(text);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ssb.setSpan(
                    new StyleSpan(Typeface.BOLD),
                    start,
                    end,
                    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
            );
        }
        binding.enterpriseCountTv.setText(ssb);
    }

    private void initView() {
        binding.topView.titleTv.setText(secondIndustry == null ? "" : secondIndustry.getName());
        binding.topView.backIv.setOnClickListener(v -> finish());

        binding.listRv.setLayoutManager(new LinearLayoutManager(this));
        DividerItemDecoration divider = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(this, R.drawable.divider_transparent));
        binding.listRv.addItemDecoration(divider);
        adapter = new IndustryEnterpriseListAdapter(this);
        adapter.setItems(pageHandler.getItems());
        binding.listRv.setAdapter(adapter);
        pageHandler.registerListener(binding.refreshListSrl);
    }

    private PageHandler<CompanyItem> pageHandler = new PageHandler<CompanyItem>() {
        @Override
        protected void onRequestData(int page, int size) {
            companyListRequest.request(IndustryEnterpriseListActivity.this, IndustryEnterpriseListActivity.this, page, size, secondIndustry.getId(), keyword);
        }
    };

    private CompanyListRequest companyListRequest = new CompanyListRequest() {
        @SuppressLint("NotifyDataSetChanged")
        @Override
        protected void onCompanyListSuccess(List<CompanyItem> companyItems, Pagination pagination) {
            pageHandler.loadDone(binding.refreshListSrl, pagination, companyItems);
            adapter.notifyDataSetChanged();

            bindEnterpriseCount(pagination.getTotal());
        }
    };

    @SubscribeEBus(receiveKey = {EBusConstants.industrySearch})
    public void onIndustrySearchEvent(String keyword) {
        this.keyword = keyword;
        pageHandler.refresh();
    }
}
